import 'dart:convert';

import 'package:hong_vpn_new_20240626/utils/global.dart';
import 'package:hong_vpn_new_20240626/utils/utils.dart';

class ChatModel {
  UserRole role;
  String content;

  String? time;

  ChatModel({required this.role, required this.content, this.time});

  ChatModel copyWith({
    UserRole? role,
    String? content,
    String? time,
  }) {
    return ChatModel(
        role: role ?? this.role,
        content: content ?? this.content,
        time: time ?? this.time);
  }

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'role': role.rawValue,
      'content': content,
      'time': time
    };
  }

  factory ChatModel.fromMap(Map<String, dynamic> map) {
    var time = Utils.getCurrentTime();
    return ChatModel(
        role: kUserRoleRawValue(map['role'] as String),
        content: map['content'] as String,
        time: map['content'] ?? time);
  }

  String toJson() => json.encode(toMap());

  factory ChatModel.fromJson(String source) =>
      ChatModel.fromMap(json.decode(source) as Map<String, dynamic>);

  @override
  String toString() => 'ChatModel(role: $role, content: $content)';

  @override
  bool operator ==(covariant ChatModel other) {
    if (identical(this, other)) return true;

    return other.role == role && other.content == content;
  }

  @override
  int get hashCode => role.hashCode ^ content.hashCode;
}
